/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Cart;
import com.click.app.model.Catalog;
import com.click.app.model.Category;
import com.click.app.model.Product;
import com.click.app.model.ProductType;
import com.click.app.model.User;
import com.click.app.service.CatalogManager;
import com.click.app.service.CategoryManager;
import com.click.app.service.ProductManager;
import com.click.app.service.ProductTypeManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author arnold
 */
public class PortalAction extends BaseAction {

    List<Category> listCategories;
    private CatalogManager catalogManager;
    private ProductManager productManager;
    private CategoryManager categoryManager;
    private ProductTypeManager productTypeManager;
    private String keyword;
    private String category;
    private String catalog;
    private List<Product> productList;
    private List<Product> productsFound;

    public String getItems() {
        Cart c = (Cart) getSession().getAttribute("cart");
        if (c != null) {
            return c.getTotalPrice().toString();
        }
        return "0.0";
    }

    public String listCatalog() {
        getRequest().setAttribute("portal", true);
        System.out.println("PortalAction -> listCatalog()");
        if (getRequest().getRemoteUser() != null) {
            User u = userManager.getUserByUsername(getRequest().getRemoteUser());
            getRequest().setAttribute("currentRole", u.getRoles().size());
        }
        Catalog catalog = catalogManager.findCurrentCatalog();
        if (catalog != null) {
            listCategories = catalog.getCategorys();
        } else {
            listCategories = new ArrayList<Category>();
        }
        for (Category category : listCategories) {
            System.out.println(category);
//            for (Product p : category.getProducts()) {
        //              System.out.println("\t" + p);
        //        }
        }
        if (productTypeManager.getAll().get(0) != null) {
            productsFound=new ArrayList<Product>();
            for (Product p : productTypeManager.getAll().get(0).getProducts()) {
                productsFound.add(p);
            }

        }

        System.out.println("PortalAction -> end listCatalog()");
        return SUCCESS;
    }

    public String editUser() {
        getRequest().setAttribute("signup", true);
        return SUCCESS;
    }

    public String viewAll() {
        productsFound = productManager.getAll();
        return SUCCESS;
    }
    private Long id;
    private Long idPt;

    public String viewByProductType() {
        productsFound = productTypeManager.get(getIdPt()).getProducts();
        return SUCCESS;
    }

    public String search() {
        if (((keyword == null) || (keyword.matches(""))) && (catalog.matches("-1"))) {
            productsFound = productManager.getAll();
            return SUCCESS;
        }
        System.out.println("searching...");
        Category cate = null;
        List<Product> products = new ArrayList<Product>();
        if (catalog.matches("-1")) {
            for (Product p : productManager.getAll()) {
                if (p.getName().toLowerCase().contains(keyword.toLowerCase())) {
                    products.add(p);
                }

            }
        } else {
            if ((keyword == null) || (keyword.matches(""))) {
                Long categoryId = Long.parseLong(category);
                cate = categoryManager.get(categoryId);
                for (ProductType pt : cate.getProductTypes()) {
                    for (Product p : pt.getProducts()) {
                        products.add(p);
                    }
                }
            } else {
                Long categoryId = Long.parseLong(category);
                cate = categoryManager.get(categoryId);
                for (ProductType pt : cate.getProductTypes()) {
                    for (Product p : pt.getProducts()) {
                        if (p.getName().toLowerCase().contains(keyword.toLowerCase())) {
                            products.add(p);
                        }

                    }
                }
            }
        }


        productsFound = products;
        System.out.println("searched...");

        return SUCCESS;
    }

    public List<Catalog> getCatalogs() {
        return catalogManager.getAll();
    }

    public List<Category> getCategorys() {
        return categoryManager.getAll();
    }

    public CatalogManager getCatalogManager() {
        return catalogManager;
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    public void setProductTypeManager(ProductTypeManager productTypeManager) {
        this.productTypeManager = productTypeManager;
    }

    public void setCategoryManager(CategoryManager categoryManager) {
        this.categoryManager = categoryManager;
    }

    public void setCatalogManager(CatalogManager catalogManager) {
        this.catalogManager = catalogManager;
    }

    public List<Category> getListCategories() {
        Catalog catalog = catalogManager.findCurrentCatalog();
        if (catalog != null) {
            listCategories = catalog.getCategorys();
        } else {
            listCategories = new ArrayList<Category>();
        }
        return listCategories;
    }

    public void setListCategories(List<Category> listCategories) {
        this.listCategories = listCategories;
    }

    /**
     * @return the keyword
     */
    public String getKeyword() {
        return keyword;
    }

    /**
     * @param keyword the keyword to set
     */
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    /**
     * @return the category
     */
    public String getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(String category) {
        this.category = category;
    }

    /**
     * @return the catalog
     */
    public String getCatalog() {
        return catalog;
    }

    /**
     * @param catalog the catalog to set
     */
    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    /**
     * @return the productList
     */
    public List<Product> getProductList() {
        return productList;
    }

    /**
     * @param productList the productList to set
     */
    public void setProductList(List<Product> productList) {
        this.productList = productList;
    }

    /**
     * @return the productsFound
     */
    public List<Product> getProductsFound() {
        return productsFound;
    }

    /**
     * @param productsFound the productsFound to set
     */
    public void setProductsFound(List<Product> productsFound) {
        this.productsFound = productsFound;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the idPt
     */
    public Long getIdPt() {
        return idPt;
    }

    /**
     * @param idPt the idPt to set
     */
    public void setIdPt(Long idPt) {
        this.idPt = idPt;
    }
    /**
     * @return the id
     */
}
